package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/setmeal")
public class SetmealController {

    // dubbo消费者
    @Reference
    SetmealService setmealService;

    // 使用JedisPoolConfig
    @Autowired
    JedisPool jedisPool;


    // 文件上传-->上传到七牛云上
    @RequestMapping(value = "/upload")
    public Result upload(@RequestParam(value = "imgFile") MultipartFile imgFile){
        try {
            // 文件名(01.jpg)
            String fileName = imgFile.getOriginalFilename();
            // 文件名不能唯一
            String uuid = UUID.randomUUID().toString().toUpperCase();
            fileName = uuid + fileName.substring(fileName.lastIndexOf("."));// SDFSDFSDFSDFSD-SDFSDFSDFSDDFSDF.jpg
            // 调用七牛云上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            // 在Controller类将图片名称存到redis中（上传七牛云图片的同时），使用集合
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            // 成功
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

    }

    // 套餐保存
    @RequestMapping(value = "/add")
    public Result add(@RequestBody Setmeal setmeal,Integer [] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
            // 成功
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }

    }

    // 分页查询套餐套餐，根据查询条件
    @RequestMapping(value = "/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean.getQueryString(),
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize());
        return pageResult;

    }
}
